package com.sheepone.util.structure.node;

/**
 * @author Miss.杨
 * @description
 * @since 2024/5/10 - 13:17
 */
public class RedBlackTreeNode<K extends Comparable<? super K>, V> extends BinarySearchTreeNode<K, V> {
    public enum Color {
        RED,
        BLACK
    }

    private Color color = Color.RED;
    private RedBlackTreeNode<K, V> parent;

    public RedBlackTreeNode(K key) {
        super(key);
    }

    public RedBlackTreeNode(K key, V value) {
        super(key, value);
    }

    public RedBlackTreeNode(V data, BinaryTreeNode<V> left, BinaryTreeNode<V> right) {
        super(data, left, right);
    }

    public RedBlackTreeNode(K key, V value, RedBlackTreeNode<K, V> parent) {
        super(key, value);
        this.parent = parent;
    }

    public RedBlackTreeNode(K key, V value, RedBlackTreeNode<K, V> parent, BinaryTreeNode<V> left, BinaryTreeNode<V> right) {
        super(key, value, left, right);
        this.parent = parent;
    }

    public RedBlackTreeNode(K key, V value, Color color, RedBlackTreeNode<K, V> parent) {
        super(key, value);
        this.color = color;
        this.parent = parent;
    }

    public RedBlackTreeNode(K key, V value, Color color, RedBlackTreeNode<K, V> parent, BinaryTreeNode<V> left, BinaryTreeNode<V> right) {
        super(key, value, left, right);
        this.color = color;
        this.parent = parent;
    }

    public Color getColor() {
        return color;
    }

    public void setColor(Color color) {
        this.color = color;
    }

    public RedBlackTreeNode<K, V> getParent() {
        return parent;
    }

    public void setParent(RedBlackTreeNode<K, V> parent) {
        this.parent = parent;
    }

    public boolean isLeftChild() {
        return this.parent != null && this.parent.getLeft() == this;
    }

    public boolean isRightChild() {
        return this.parent != null && this.parent.getRight() == this;
    }

    /**
     * 获取兄弟节点
     *
     * @return 兄弟节点
     */
    public RedBlackTreeNode<K, V> sibling() {
        if (this.parent == null) {
            return null;
        }
        return isLeftChild() ? (RedBlackTreeNode<K, V>) this.parent.getRight() : (RedBlackTreeNode<K, V>) this.parent.getLeft();
    }

    /**
     * 获取叔叔节点:父亲的兄弟节点
     *
     * @return 叔叔节点
     */
    public RedBlackTreeNode<K, V> uncle() {
        if (this.parent == null || this.parent.parent == null) {
            return null;
        }
        if (this.parent.isLeftChild()) {
            return (RedBlackTreeNode<K, V>) this.parent.parent.getRight();
        } else {
            return (RedBlackTreeNode<K, V>) this.parent.parent.getLeft();
        }
    }

    public static <K extends Comparable<? super K>, V> boolean isRed(RedBlackTreeNode<K, V> node) {
        return node != null && node.getColor() == RedBlackTreeNode.Color.RED;
    }

    public static <K extends Comparable<? super K>, V> boolean isBlack(RedBlackTreeNode<K, V> node) {
        return node == null || node.getColor() == RedBlackTreeNode.Color.BLACK;
    }

}
